package com.maori.languageapp.curate.word;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.maori.languageapp.R;
import com.maori.languageapp.consts.ImageLoader;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Maori_Curate_ImageList_Word_Adapter extends BaseAdapter {
	private static final String TAG = null;
	// Declare Variables
	Context context;
	LayoutInflater inflater;
	ArrayList<HashMap<String, String>> data;
	ImageLoader imageLoader;
	String ApproveState,AudioMediaRefNo,UserCurateStateImage,MediaUID,NodeMediaType,SynUserID,SynNodeId,SynParentNodeId,SynMediaType,DisplayImageSource,ImageParentUID,ImageParentNID,UserLikedImage,result,ImageMediaID,TotalsSetDefaultImage;
	Button setDefAction,setLikeAction;
	int timeoutConnection = 5000;
	HttpParams httpParameters = new BasicHttpParams();
	InputStream is = null;
	StringBuilder sb = null;
	private LayoutInflater mInflater;
	HashMap<String, String> resultp = new HashMap<String, String>();
	
	public Maori_Curate_ImageList_Word_Adapter(Context context,
			ArrayList<HashMap<String, String>> arraylist) {
		this.context = context;
		data = arraylist;
		imageLoader = new ImageLoader(context);
		mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		 return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// Declare Variables
		
		final ViewHolder holder;
		 if (convertView == null) {
		        convertView = mInflater.inflate(R.layout.languageapp_curate_image, null);
		        holder = new ViewHolder();
		        holder.first_name = (TextView) convertView.findViewById(R.id.first_name);
		        holder.user_role = (TextView) convertView.findViewById(R.id.userrole);
		        holder.display_dialect = (Button) convertView.findViewById(R.id.display_dialect);
		        holder.display_location = (Button) convertView.findViewById(R.id.display_location);
		        holder.userimage = (ImageView) convertView.findViewById(R.id.userimggriditem);
		        holder.flag = (ImageView) convertView.findViewById(R.id.ImageView_example);
		        holder.setApproveAction = (Button) convertView.findViewById(R.id.setApproveAction);
		        holder.setRejectAction = (Button) convertView.findViewById(R.id.setRejectAction);
		        holder.setModifyAction = (Button) convertView.findViewById(R.id.setModifyAction);
		        holder.urlreader = (Button) convertView.findViewById(R.id.urlreader);
		        
		     
		        convertView.setTag(holder);
		    } else {
		        holder = (ViewHolder) convertView.getTag();
		    }


		resultp = data.get(position);
		

		String NodeMainImage = resultp.get(Maori_Curate_Word_Details.getIMAGEM_Source());
		if(NodeMainImage.equals("404")){
			DisplayImageSource = "http://langapp.mirzabros.com/images/placeholder.png";
		}
		else{
			DisplayImageSource = resultp.get(Maori_Curate_Word_Details.getIMAGEM_Source());
		}
		
		imageLoader.DisplayImage(DisplayImageSource ,holder.flag);
		imageLoader.DisplayImage(resultp.get(Maori_Curate_Word_Details.getIMAGEMID()),holder.userimage);
		holder.first_name.setText(resultp.get(Maori_Curate_Word_Details.getIMAGEfirst_name()) + " " +resultp.get(Maori_Curate_Word_Details.getIMAGElast_name()));
		holder.user_role.setText(resultp.get(Maori_Curate_Word_Details.getSYNtribe()));
		holder.display_dialect.setText(resultp.get(Maori_Curate_Word_Details.getImageSyndialact()));
		holder.display_location.setText(resultp.get(Maori_Curate_Word_Details.getSYNcurrent_location()));
	
		MediaUID = resultp.get(Maori_Curate_Word_Details.getMediaUID());
		NodeMediaType = resultp.get(Maori_Curate_Word_Details.getNodeMediaType());
		
		ImageMediaID = resultp.get(Maori_Curate_Word_Details.getIMAGENM_ID());
		UserLikedImage = resultp.get(Maori_Curate_Word_Details.getUserLikedImage());
		ImageParentNID = resultp.get(Maori_Curate_Word_Details.getImageParentNID());
		ImageParentUID = resultp.get(Maori_Curate_Word_Details.getImageParentUID());
		TotalsSetDefaultImage = resultp.get(Maori_Curate_Word_Details.getTotalsSetDefaultImage());
		UserCurateStateImage = resultp.get(Maori_Curate_Word_Details.getUserCurateStateImage());
		/********** if user liked Like Button Set to Active ******************/

		if(UserCurateStateImage.equals("1")){
			holder.setApproveAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_default_active, 0, 0, 0);
			holder.setApproveAction.setEnabled(false);
		}
		else if(UserCurateStateImage.equals("2")){
			holder.setRejectAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_reject_active, 0, 0, 0);
			holder.setRejectAction.setEnabled(false);
		}
		else if(UserCurateStateImage.equals("3")){
			holder.setModifyAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_modify_active, 0, 0, 0);
			holder.setModifyAction.setEnabled(false);
		}
		
		
		holder.setApproveAction.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				resultp = data.get(position);
				SynUserID = resultp.get(Maori_Curate_Word_Details.getUIDuser());
				SynNodeId = resultp.get(Maori_Curate_Word_Details.getSYNNID());
				ImageParentNID = resultp.get(Maori_Curate_Word_Details.getImageParentNID());
				AudioMediaRefNo = resultp.get(Maori_Curate_Word_Details.getIMAGENM_ID());
				SynMediaType = "1";
				ApproveState = "1";
				
				holder.setApproveAction.setEnabled(false);
				holder.setApproveAction.setText("Approve");
				holder.setApproveAction.setTextColor(Color.parseColor("#07afdf"));
				holder.setApproveAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_default_active, 0, 0, 0);
				
				holder.setRejectAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_reject_inactive, 0, 0, 0);
				holder.setRejectAction.setTextColor(Color.parseColor("#626262"));
				holder.setRejectAction.setEnabled(true);
				holder.setModifyAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_modify_inactive, 0, 0, 0);
				holder.setModifyAction.setTextColor(Color.parseColor("#626262"));
				holder.setModifyAction.setEnabled(true);
				
				
				new UploadApprovalInformation().execute();
			}
		});
		
		
		holder.setRejectAction.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				resultp = data.get(position);
				SynUserID = resultp.get(Maori_Curate_Word_Details.getUIDuser());
				SynNodeId = resultp.get(Maori_Curate_Word_Details.getSYNNID());
				ImageParentNID = resultp.get(Maori_Curate_Word_Details.getImageParentNID());
				AudioMediaRefNo = resultp.get(Maori_Curate_Word_Details.getIMAGENM_ID());
				SynMediaType = "1";
				ApproveState = "2";
				final Dialog dialog=new Dialog(context);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.languageapp_curate_reject_form);
				
				Button rejectaccept=(Button)dialog.findViewById(R.id.rejectaccept);
				rejectaccept.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						
						holder.setRejectAction.setEnabled(false);
						holder.setRejectAction.setText("Reject");
						holder.setRejectAction.setTextColor(Color.parseColor("#07afdf"));
						holder.setRejectAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_reject_active, 0, 0, 0);
						
						holder.setApproveAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_default_inactive, 0, 0, 0);
						holder.setApproveAction.setTextColor(Color.parseColor("#626262"));
						holder.setApproveAction.setEnabled(true);
						holder.setModifyAction.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_modify_inactive, 0, 0, 0);
						holder.setModifyAction.setTextColor(Color.parseColor("#626262"));
						holder.setModifyAction.setEnabled(true);

						new UploadApprovalInformation().execute();
						dialog.dismiss();
					}
				});
				
				Button rejectcancel=(Button)dialog.findViewById(R.id.rejectcancel);
				rejectcancel.setOnClickListener(new View.OnClickListener() {
					public void onClick(View view) {
						dialog.dismiss();
					}
				});
				dialog.show();
			}
		});
		
		
		return convertView;
	}
	
	
	static class ViewHolder {
		Button setRejectAction;
		Button setModifyAction;
		Button setApproveAction;
		Button urlreader;
		TextView first_name;
		TextView last_name;
		TextView  user_role;
		ImageView userimage;
		Button display_location;
		Button display_dialect;
		ImageView flag;

	}
	
	/*********************************************/
	
	private class UploadApprovalInformation extends AsyncTask<Void, Void, Void> {
		private final String TAG = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			try {
				HttpPost httppost = null;
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				JSONObject json = new JSONObject();
				httppost = new HttpPost(
						"http://langapp.mirzabros.com/maoriapi/curate/SaveCurateApprovalMediaInfo.php");

				json.put("record1", ImageParentUID); // $approve_user
				json.put("record2", ApproveState); // $approve_state
				json.put("record3", ImageParentNID); // $approve_node
				json.put("record4", SynMediaType); // $approve_media
				json.put("record5", AudioMediaRefNo); // $approve_media_id
	
				JSONArray postjson = new JSONArray();
				postjson.put(json);
				Log.v("TAG","POST SET DEF : " + postjson);
				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
					Log.v("TAG", "Print RES ***:" + result);
					if (result != null) {

					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void args) {
			
		}
	}
	
	
}
